home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / generic / tclIOSock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  2.6 KB  |  103 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tclIOSock.c --
  3.  *
  4.  *    Common routines used by all socket based channel types.
  5.  *
  6.  * Copyright (c) 1995 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * SCCS: @(#) tclIOSock.c 1.20 97/04/25 16:36:40
  12.  */
  13.  
  14. #include "tclInt.h"
  15. #include "tclPort.h"
  16.  
  17. /*
  18.  *----------------------------------------------------------------------
  19.  *
  20.  * TclSockGetPort --
  21.  *
  22.  *    Maps from a string, which could be a service name, to a port.
  23.  *    Used by socket creation code to get port numbers and resolve
  24.  *    registered service names to port numbers.
  25.  *
  26.  * Results:
  27.  *    A standard Tcl result.  On success, the port number is
  28.  *    returned in portPtr. On failure, an error message is left in
  29.  *    interp->result.
  30.  *
  31.  * Side effects:
  32.  *    None.
  33.  *
  34.  *----------------------------------------------------------------------
  35.  */
  36.  
  37. int
  38. TclSockGetPort(interp, string, proto, portPtr)
  39.     Tcl_Interp *interp;
  40.     char *string;        /* Integer or service name */
  41.     char *proto;        /* "tcp" or "udp", typically */
  42.     int *portPtr;        /* Return port number */
  43. {
  44.     struct servent *sp;        /* Protocol info for named services */
  45.     if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
  46.     sp = getservbyname(string, proto);    
  47.     if (sp != NULL) {
  48.         *portPtr = ntohs((unsigned short) sp->s_port);
  49.         Tcl_ResetResult(interp);    /* clear error message */
  50.         return TCL_OK;
  51.     }
  52.     return TCL_ERROR;
  53.     }
  54.     if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
  55.     return TCL_ERROR;
  56.     }
  57.     if (*portPtr > 0xFFFF) {
  58.         Tcl_AppendResult(interp, "couldn't open socket: port number too high",
  59.                 (char *) NULL);
  60.     return TCL_ERROR;
  61.     }
  62.     return TCL_OK;
  63. }
  64.  
  65. /*
  66.  *----------------------------------------------------------------------
  67.  *
  68.  * TclSockMinimumBuffers --
  69.  *
  70.  *    Ensure minimum buffer sizes (non zero).
  71.  *
  72.  * Results:
  73.  *    A standard Tcl result.
  74.  *
  75.  * Side effects:
  76.  *    Sets SO_SNDBUF and SO_RCVBUF sizes.
  77.  *
  78.  *----------------------------------------------------------------------
  79.  */
  80.  
  81. int
  82. TclSockMinimumBuffers(sock, size)
  83.     int sock;            /* Socket file descriptor */
  84.     int size;            /* Minimum buffer size */
  85. {
  86.     int current;
  87.     int len;
  88.     
  89.     len = sizeof(int);
  90.     getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)¤t, &len);
  91.     if (current < size) {
  92.     len = sizeof(int);
  93.     setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
  94.     }
  95.     len = sizeof(int);
  96.     getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)¤t, &len);
  97.     if (current < size) {
  98.     len = sizeof(int);
  99.     setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
  100.     }
  101.     return TCL_OK;
  102. }
  103.